<?php
Globals::requireController('Admin');
Globals::requireTable('Advertising');

class AdvertisingController extends AdminController
{
    protected $AdvertisingTable;
    
	public function __construct($config = null)
	{
		parent::__construct($config, true);
		$this->AdvertisingTable = new AdvertisingTable($config);
	}
	
	public function indexAction()
	{
	    $machine_type = $this->getIntParam('machine_type');
	    $where = array('status'=>1,'machine_type'=>$machine_type);
	    $count = $this->AdvertisingTable->listCount($where);
	    $pageSize = 20;
	    $pageId	= $this->getIntParam('page');
	    $this->view->paging = $this->getPaging($count, $pageSize,$pageId);
	    $this->view->data = $this->AdvertisingTable->listPage($where, 'id ASC', $pageSize, $pageId);
	    $this->view->machine_type = $machine_type;
	}
	
	public function addAction()
	{
	    $machine_type = $this->getIntParam('machine_type');
	    $id = $this->getIntParam('id');
	    if($id > 0){
	        $title = $this->AdvertisingTable->getRow($id);
	        if($title['id'] > 0){
	            $this->view->data = $title;
	        }
	    }
	    $this->view->machine_type = $machine_type;
	}
	
	public function doaddAction()
	{
	    $id = $this->getIntParam('id');
	    $data['type'] = $this->getParam('type');
	    $data['contenturl'] = $this->getParam('contenturl');
	    $data['openurl'] = $this->getParam('openurl');
	    $data['contentsize'] = $this->getParam('contentsize');
	    $data['duration'] = $this->getParam('duration');
	    $data['closeflag'] = $this->getParam('closeflag');
	    $data['machine_type'] = $this->getParam('machine_type');
	    $data['createdate'] = date('Y-m-d H:i:s');
	    if($id > 0){
	        $this->AdvertisingTable->update($data,$id);
	    }else{
            $data['status'] = 1;
            $this->AdvertisingTable->add($data);
	    }
	    $this->redirectToAction('index',array('machine_type'=>$data['machine_type']));
	    exit();
	}
	
	public function deleteAction()
	{
	    $maching_type = $this->getIntParam('machine_type');
	    $id = $this->getIntParam('id');
	    $this->AdvertisingTable->delete($id);
	    $this->redirectToAction('index',array('machine_type'=>$maching_type));
	    exit();
	}
	
}
